//
//  SaveTool.m
//  UI19_封装存本地
//
//  Created by dllo on 15/10/13.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "SaveTool.h"

@implementation SaveTool

+ (void)isHaveFolder{
//    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //1.找沙盒路径
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    //2.拼接电影
    NSString *movieFile = [sandBoxPath stringByAppendingPathComponent:@"Collect"];
    //创建
//    NSFileManager *manager = [NSFileManager defaultManager];
    NSFileManager *manager = [NSFileManager defaultManager];
    //判断
    //通过管理者调用方法,根据路径判断文件夹是否存在
    if (![manager fileExistsAtPath:movieFile]) {
        [manager createDirectoryAtPath:movieFile withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSLog(@"%@", movieFile);
    
}

+ (BOOL)isHaveMovieInPlist:(ZGSDestinationCity *)movie{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Collect/movie.plist"];
    //反归档
    NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    //遍历
    for (ZGSDestinationCity *temp in movieArr) {
        NSLog(@"%@", temp.idd);
        NSLog(@"%@", movie.idd);
        if ([temp.idd isEqualToString:movie.idd]) {
            return NO;
        }
    }
    return YES;
}

+ (void)saveMovieToPlist:(ZGSDestinationCity *)movie{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Collect/movie.plist"];
    //反归档
    NSMutableArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    if (movieArr == nil) {
        movieArr = [NSMutableArray array];
        
    }
    [movieArr addObject:movie];
    //归档
     [NSKeyedArchiver archiveRootObject:movieArr toFile:docPath];
    NSLog(@"%@", docPath);
    
}
+ (void)cancleMovieInPlist:(ZGSDestinationCity *)movie{
    //路径
    NSString *sandBoxpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *docPath = [sandBoxpath stringByAppendingPathComponent:@"Collect/movie.plist"];
    //反归档
    NSMutableArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
    //遍历已初步
    for (ZGSDestinationCity *temp in movieArr) {
        if ([temp.idd isEqualToString:movie.idd ]) {
            [movieArr removeObject:temp];
            break;
            
        }
    }
    //归档
    [NSKeyedArchiver archiveRootObject:movieArr toFile:docPath];
    
    
}
+ (BOOL)ishaveInFile:(id)object type:(SaveToolType)type{
    NSString *sandBoxPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    switch (type) {
        case SaveToolMovie:{
            //拼接电影的文件夹路径
            NSString *docPath = [sandBoxPath stringByAppendingPathComponent:@"Collect/movie.plist"];
            NSArray *movieArr = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
            ZGSDestinationCity *movie = object;
            for (ZGSDestinationCity *temp in movieArr) {
                if ([temp.idd isEqualToString:movie.idd]) {
                    return NO;
                }
            }
        }
            break;
            
        default:
            break;
    }
    return YES;
}

@end
